//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.aisoar.magicsoar.api.common.base;

import com.alibaba.fastjson.JSON;
import com.aisoar.magicsoar.api.common.base.BasePageParam;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = -9122598292287970890L;
    private int code;
    private String message;
    private Boolean success;
    private String extraMessage;
    private T data;

    public ApiResult() {
        this.setCode(ApiResult.ResultCode.OK.getCode());
        this.setSuccess(true);
    }

    public ApiResult(T data) {
        this.setCode(ApiResult.ResultCode.OK.getCode());
        this.data = data;
    }

    public ApiResult(int code, String message) {
        this.setCode(code);
        this.message = message;
    }

    public ApiResult(ResultCode code, String message) {
        this.setCode(code.getCode());
        this.setMessage(code.getDesc());
        if (StringUtils.isNotBlank(message)) {
            this.message = message;
        }

    }

    public ApiResult(ResultCode code, String message, Boolean success) {
        this.setCode(code.getCode());
        this.message = message;
        this.success = success;
    }

    public ApiResult(String message) {
        this.message = message;
    }

    public ApiResult(int code, String message, T data) {
        this.setCode(code);
        this.message = message;
        this.data = data;
    }

    public ApiResult(ResultCode code, String message, T data) {
        this.setCode(code.getCode());
        this.message = message;
        this.data = data;
    }

    public ApiResult(String message, T data) {
        this.message = message;
        this.data = data;
    }

    public static final <T> ApiResult<T> buildSuccessResult() {
        return new ApiResult(ApiResult.ResultCode.OK, "", true);
    }

    public static final <T> ApiResult<T> buildSuccessResult(T data) {
        ApiResult<T> apiResult = new ApiResult(ApiResult.ResultCode.OK, "", true);
        apiResult.setData(data);
        return apiResult;
    }

    public static final <T> ApiResult<T> buildFailResult(ResultCode resultCode, String message) {
        ApiResult<T> apiResult = new ApiResult(resultCode, message);
        apiResult.setSuccess(false);
        return apiResult;
    }

    public static final <T> ApiResult<T> of(int code, String msg, T data) {
        return new ApiResult(code, msg, data);
    }

    public static final <T> ApiResult<T> of(ResultCode code, String msg, T data) {
        return new ApiResult(code, msg, data);
    }

    public static <T> ApiResult<PageData<T>> page(List<T> rows, long totalRows, int totalPage, BasePageParam param) {
        ApiResult<PageData<T>> result = new ApiResult();
        PageData<T> pageData = new PageData();
        pageData.setRows(rows);
        pageData.setTotalRows(totalRows);
        pageData.setTotalPage(totalPage);
        pageData.setPageNo(param.getPageNo());
        pageData.setPageSize(param.getPageSize());
        result.setData(pageData);
        return result;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getSuccess() {
        return this.success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getExtraMessage() {
        return this.extraMessage;
    }

    public void setExtraMessage(String extraMessage) {
        this.extraMessage = extraMessage;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public String toJSONString() {
        return this.toString();
    }

    public static enum ResultCode {
        OK(200, ""),
        NOTRELUST(201, ""),
        BAD_REQUEST(400, ""),
        UNAUTHORIZED(401, ""),
        FORBIDDEN(403, ""),
        NOT_FOUND(404, ""),
        CONFLICT(409, ""),
        TOO_MANY_REQUESTS(429, ""),
        INTERNAL_SERVER_ERROR(500, "服务繁忙"),
        SERVICE_UNAVAILABLE(503, "");

        private Integer code;
        private String desc;

        private ResultCode(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public static boolean isOK(int code) {
            return OK.getCode() == code;
        }

        public Integer getCode() {
            return this.code;
        }

        public String getDesc() {
            return this.desc;
        }
    }
}
